﻿#region zh-CHS 2006 - 2008 DemoSoft 团队 | en 2006 - 2008 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2008 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Threading;
using Demo.Mmose.Core.Util;
#endregion

namespace Demo.Mmose.Core.Common.Atom
{
    /// <summary>
    /// 
    /// </summary>
    public class AtomicFloat : IAtomOperator<float>, IComparable, IComparable<AtomicFloat>
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        public AtomicFloat( )
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="atomicFloat"></param>
        public AtomicFloat( float atomicFloat )
        {
            m_AtomicFloat = atomicFloat;
        }
        #endregion

        #region zh-CHS 属性 | en Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private float m_AtomicFloat = 0;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public float Value
        {
            get { return m_AtomicFloat; }
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 以原子方式将给定值与当前值相加。
        /// </summary>
        /// <param name="addValue"></param>
        /// <returns></returns>
        public void Add( float addValue )
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = m_AtomicFloat + addValue;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 以原子方式将给定值与当前值相减。
        /// </summary>
        /// <param name="subtractValue"></param>
        /// <returns></returns>
        public void Subtract( float subtractValue )
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = m_AtomicFloat - subtractValue;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 以原子方式将给定值与当前值相乘。
        /// </summary>
        /// <param name="multiplyValue"></param>
        /// <returns></returns>
        public void Multiply( float multiplyValue )
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = m_AtomicFloat * multiplyValue;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 以原子方式将给定值与当前值相除。
        /// </summary>
        /// <param name="divideValue"></param>
        /// <returns></returns>
        public void Divide( float divideValue )
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = m_AtomicFloat / divideValue;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 以原子方式设置为给定值。
        /// </summary>
        /// <param name="assignmentValue"></param>
        /// <returns></returns>
        public void Assignment( float assignmentValue )
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = assignmentValue;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 如果当前值 == 预期值，则以原子方式将该值设置为给定的更新值。
        /// </summary>
        /// <param name="setValue"></param>
        /// <param name="compareValue"></param>
        /// <returns></returns>
        public bool CompareExchange( float setValue, float compareValue )
        {
            return Interlocked.CompareExchange( ref m_AtomicFloat, setValue, compareValue ) == compareValue;
        }

        /// <summary>
        /// 以原子方式将当前值减 1 ( -- )
        /// </summary>
        /// <returns></returns>
        public void Decrement()
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = m_AtomicFloat - 1.0F;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 以原子方式将当前值加 1 ( ++ )
        /// </summary>
        /// <returns></returns>
        public void Increment()
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = m_AtomicFloat + 1.0F;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 以原子方式将当前值左移 ( << )
        /// </summary>
        /// <returns></returns>
        public void LeftShift( int leftShift )
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// 以原子方式将当前值右移 ( >> )
        /// </summary>
        /// <returns></returns>
        public void RightShift( int rightShift )
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// 以原子方式将当前值求正(一元操作) ( + )
        /// </summary>
        /// <param name="addValue"></param>
        /// <returns></returns>
        public void Plus()
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = +m_AtomicFloat;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 以原子方式将当前值求负(一元操作) ( - )
        /// </summary>
        /// <param name="addValue"></param>
        /// <returns></returns>
        public void Minus()
        {
            float oldAtomicFloat;
            float newAtomicFloat;

            do
            {
                oldAtomicFloat = m_AtomicFloat;
                newAtomicFloat = -m_AtomicFloat;

            } while ( Interlocked.CompareExchange( ref m_AtomicFloat, newAtomicFloat, oldAtomicFloat ) != oldAtomicFloat );
        }

        /// <summary>
        /// 以原子方式将当前值取反(一元操作) ( ! )
        /// </summary>
        /// <param name="addValue"></param>
        /// <returns></returns>
        public void Invert()
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// 以原子方式将当前值求补 ( ~ )
        /// </summary>
        /// <returns></returns>
        public void OneComplement()
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// 以原子方式将当前值按位与 ( &= )
        /// </summary>
        /// <returns></returns>
        public void BitwiseAND( float bitwiseAnd )
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// 以原子方式将当前值按位或 ( |= )
        /// </summary>
        /// <returns></returns>
        public void BitwiseInclusiveOR( float inclusiveOR )
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// 以原子方式将当前值按位异或 ( ^= )
        /// </summary>
        /// <returns></returns>
        public void BitwiseExclusiveOR( float exclusiveOR )
        {
            throw new NotSupportedException();
        }
        #endregion

        #region zh-CHS 共有委托 | en Public Delegate
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<UpdatingValue<float>> m_EventUpdatingValue;
        /// <summary>
        /// 
        /// </summary>
        private SpinLock m_LockEventUpdating = new SpinLock();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<UpdatingValue<float>> UpdatingValue
        {
            add
            {
                SpinLockEx.ReliableEnter( ref m_LockEventUpdating );
                {
                    m_EventUpdatingValue += value;
                }
                m_LockEventUpdating.Exit();
            }
            remove
            {
                SpinLockEx.ReliableEnter( ref m_LockEventUpdating );
                {
                    m_EventUpdatingValue -= value;
                }
                m_LockEventUpdating.Exit();
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<UpdatedValue<float>> m_EventUpdatedValue;
        /// <summary>
        /// 
        /// </summary>
        private SpinLock m_LockEventUpdated = new SpinLock();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<UpdatedValue<float>> UpdatedValue
        {
            add
            {
                SpinLockEx.ReliableEnter( ref m_LockEventUpdated );
                {
                    m_EventUpdatedValue += value;
                }
                m_LockEventUpdated.Exit();
            }
            remove
            {
                SpinLockEx.ReliableEnter( ref m_LockEventUpdated );
                {
                    m_EventUpdatedValue -= value;
                }
                m_LockEventUpdated.Exit();
            }
        }
        #endregion

        #region zh-CHS 方法 | en Method
        /// <summary>
        /// 将给定值与当前值相加，并返回相加后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator +( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat + atomicFloatB );
        }

        /// <summary>
        /// 将给定值与当前值相加，并返回相加后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator +( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat + atomicFloatB.m_AtomicFloat );
        }

        /// <summary>
        /// 将给定值与当前值相减，并返回相减后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator -( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat - atomicFloatB );
        }

        /// <summary>
        /// 将给定值与当前值相减，并返回相减后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator -( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat - atomicFloatB.m_AtomicFloat );
        }

        /// <summary>
        /// 将给定值与当前值相乘，并返回相乘后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator *( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat * atomicFloatB );
        }

        /// <summary>
        /// 将给定值与当前值相乘，并返回相乘后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator *( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat * atomicFloatB.m_AtomicFloat );
        }

        /// <summary>
        /// 将当前值与给定值相除，并返回相除后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator /( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat / atomicFloatB );
        }

        /// <summary>
        /// 将当前值与给定值相除，并返回相除后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator /( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat / atomicFloatB.m_AtomicFloat );
        }

        /// <summary>
        /// 将当前值与给定值相模，并返回相模后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator %( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat % atomicFloatB );
        }

        /// <summary>
        /// 将当前值与给定值相模，并返回相模后的数据
        /// </summary>
        /// <param name="atomicFloatA"></param>
        /// <param name="atomicFloatB"></param>
        /// <returns></returns>
        public static AtomicFloat operator %( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return new AtomicFloat( atomicFloatA.m_AtomicFloat % atomicFloatB.m_AtomicFloat );
        }

        /// <summary>
        /// 将当前值求正，并返回求正后的数据
        /// </summary>
        /// <param name="atomicIntA"></param>
        /// <returns></returns>
        public static AtomicFloat operator +( AtomicFloat atomicFloat )
        {
            return new AtomicFloat( +atomicFloat.m_AtomicFloat );
        }

        /// <summary>
        /// 将当前值求负，并返回求负后的数据
        /// </summary>
        /// <param name="atomicIntA"></param>
        /// <returns></returns>
        public static AtomicFloat operator -( AtomicFloat atomicFloat )
        {
            return new AtomicFloat( -atomicFloat.m_AtomicFloat );
        }

        /// <summary>
        /// 以原子方式将当前值加 1 ( ++ )
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static AtomicFloat operator ++( AtomicFloat atomicFloat )
        {
            atomicFloat.Increment();

            return atomicFloat;
        }

        /// <summary>
        /// 以原子方式将当前值减 1 ( -- )
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static AtomicFloat operator --( AtomicFloat atomicFloat )
        {
            atomicFloat.Decrement();

            return atomicFloat;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator ==( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat == atomicFloatB.m_AtomicFloat;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator ==( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat == atomicFloatB;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator !=( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat != atomicFloatB.m_AtomicFloat;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator !=( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat != atomicFloatB;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator <( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat < atomicFloatB.m_AtomicFloat;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator <( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat < atomicFloatB;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator >( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat > atomicFloatB.m_AtomicFloat;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator >( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat > atomicFloatB;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator <=( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat <= atomicFloatB.m_AtomicFloat;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator <=( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat <= atomicFloatB;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator >=( AtomicFloat atomicFloatA, AtomicFloat atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat >= atomicFloatB.m_AtomicFloat;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator >=( AtomicFloat atomicFloatA, float atomicFloatB )
        {
            return atomicFloatA.m_AtomicFloat >= atomicFloatB;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iSerial"></param>
        /// <returns></returns>
        public static explicit operator float( AtomicFloat atomicFloat )
        {
            return atomicFloat.m_AtomicFloat;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="atomicFloat"></param>
        /// <returns></returns>
        public static implicit operator AtomicFloat( float atomicFloat )
        {
            return new AtomicFloat( atomicFloat );
        }
        #endregion

        #region zh-CHS 方法覆盖 | en Method Overrides
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return m_AtomicFloat.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return m_AtomicFloat.GetHashCode();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xObject"></param>
        /// <returns></returns>
        public override bool Equals( object xObject )
        {
            AtomicFloat atomicFloat = xObject as AtomicFloat;
            if ( atomicFloat == null )
                return false;

            return atomicFloat.m_AtomicFloat == m_AtomicFloat;
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 
        /// </summary>
        /// <param name="otherAtomicInt"></param>
        /// <returns></returns>
        public int CompareTo( AtomicFloat otherAtomicFloat )
        {
            return m_AtomicFloat.CompareTo( otherAtomicFloat.m_AtomicFloat );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="otherObject"></param>
        /// <returns></returns>
        public int CompareTo( object otherObject )
        {
            AtomicFloat atomicFloat = otherObject as AtomicFloat;
            if ( atomicFloat == null )
                return 1;
            else
                return CompareTo( atomicFloat );
        }
        #endregion
    }
}
#endregion